<template>
<div class="home">
  <div class="homeOne">
    <div class="searchMain">
      <div class="search">
        <span>成都市</span>
        <van-badge :content="5">
          <div class="child">
            <i class="iconfont" @click="go_massage">&#xe689;</i>
          </div>
        </van-badge>
      </div>
      <van-search
        @click="goSearch"
        v-model="value"
        shape="round"
        background="white"
        placeholder="请输入搜索关键词"
      />
    </div>
    <div class="home_nav">
      <van-tabs>
        <van-tab
          :key="item.id"
          v-for="item in navdata"
          :title="item.name"
        ></van-tab>
      </van-tabs>
    </div>
    <van-swipe :autoplay="3000" :height="150">
      <van-swipe-item v-for="item in banners" :key="item">
        <img v-lazy="item" :style="{ width: '100%', height: '100%' }" />
      </van-swipe-item>
    </van-swipe>
    <div class="home_tips">
      <van-icon name="volume" color="red" size="18px" />
      <van-swipe style="height: 20px" vertical :autoplay="3000" :show-indicators="false">
        <van-swipe-item>天鹅堡商城自营配送系统上线</van-swipe-item>
        <van-swipe-item>天鹅堡商城上市新品优享，满88元包邮</van-swipe-item>
        <van-swipe-item>天鹅堡商城售后退换无忧服务上线</van-swipe-item>
        <van-swipe-item>天鹅堡商城成为中国最大生鲜交易平台</van-swipe-item>
      </van-swipe>
    </div>
    <div class="home_main">
      <ul>
        <li :key="item.id" v-for="item in homeMain" @click="toDetail(item.id)">
          <img :src="item.image" alt="" />
          <p>{{ item.title }}</p>
          <span>￥ {{ item.originPrice }}</span> <b>￥{{ item.price }}</b>
        </li>
      </ul>
    </div>
  </div>
</div>
</template>

<script>
import { getNav, getBanner, getMain, getDetail } from '@/api/home'
export default {
  name: 'home',
  data() {
    return {
      navdata: [],
      banners: [],
      homeMain: [],
      value: '',
      nextIndex: 0,
      isEnd: false,
      products: [],
    }
  },
  created() {
    this.getNavs()
    this.getBanners()
    this.getMains()
    this.getDetails()
  },
  methods: {
    go_massage() {
      this.$router.push({ path: '/news' })
    },
    goSearch() {
      this.$router.push({ path: '/search' })
    },
    toDetail(data) {
      this.$router.push({ path: '/detail', query: { id: data } })
    },
    async getNavs() {
      const { list: navdata } = await getNav()
      this.navdata = navdata
    },
    async getBanners() {
      const { url: banners } = await getBanner()
      this.banners = banners
      // console.log(banners)
    },
    async getMains() {
      const { list: homeMain } = await getMain()
      this.homeMain = homeMain
    },
    async getDetails() {
      const { list: products, isEnd, nextIndex } = await getDetail()
      this.isEnd = isEnd
      this.products = products
      this.nextIndex = nextIndex
      // console.log(list)s
    },
    async onLoad() {
      // 异步更新数据
      const { list, nextIndex, isEnd } = await getDetail(this.nextIndex)
      this.products = [
        ...this.products,
        ...list,
      ]
      this.nextIndex = nextIndex
      this.isEnd = isEnd
      this.loading = false
    },
  },
}
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
@font-face {
  font-family: "iconfont"; /* project id 3284801 */
  src: url("");
  src: url("?#iefix") format("embedded-opentype"),
    url("//at.alicdn.com/t/font_3284801_q7x5k1gyrcj.woff2") format("woff2"),
    url("//at.alicdn.com/t/font_3284801_q7x5k1gyrcj.woff") format("woff"),
    url("//at.alicdn.com/t/font_3284801_q7x5k1gyrcj.ttf") format("truetype"),
    url("#iconfont") format("svg");
}
.iconfont {
  font-family: "iconfont" !important;
  font-size: 25px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -webkit-text-stroke-width: 0.2px;
  -moz-osx-font-smoothing: grayscale;
}
.home{
  width: 100%;
  height: 100%;
  .homeOne{
    padding: 0 10px;
    box-sizing: border-box;
    background-color: rgba(249, 249, 249, 1);
    .search {
  width: 100%;
  height: 40px;
  font-size: 16px;
  color: #333333;
  background-color: #f9f9f9;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  box-sizing: border-box;
}
.child {
  width: 25px;
  height: 25px;
  background: #f2f3f5;
  border-radius: 4px;
}
.home_nav {
  width: 100%;
  height: 40px;
  padding: 0 16px;
  box-sizing: border-box;
  background-color: white;
  border: 1px solid #f2f2f2;
}
.home_tips {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #dedede;
  // justify-content: center;
  .van-icon{
    padding-left: 20px;
    padding-right: 10px;
    box-sizing: border-box;
  }
  .van-swipe__track,.van-swipe__track--vertical{
     width: 100%;
     height: 40px;
     display: flex;
     align-items: center;
     .van-swipe-item{
      width: 100%;
      height: 20px;
      display: flex;
      align-items: center;
     }
  }
}
.home_main {
  width: 100%;
  padding: 0 16px;
  margin-top: 10px;
  box-sizing: border-box;
  ul {
    width: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    li {
      width: 160px;
      height: 200px;
      margin-bottom: 30px;
      img {
        width: 160px;
        height: 160px;
      }
      p {
        font-size: 12px;
        color: #666666;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        margin: 8px 0;
      }
      span {
        color: #f04844;
        font-size: 12px;
      }
      b {
        color: #999999;
        font-size: 12px;
        font-weight: 300;
        text-decoration: line-through;
        margin-left: 8px;
      }
    }
  }
}
  }
}

</style>
